<template>
  <div>
    <!-- 导航 -->
    <van-nav-bar class="navbar">
      <template #title>
        <van-button
          type="info"
          icon="search"
          round
          class="search-btn"
          @click="$router.push('/search')"
          >搜索</van-button
        >
      </template>
      <!-- tabs -->
      <!-- active控制高亮 ，属性值是tab的索引-->
    </van-nav-bar>
    <van-tabs v-model="active" swipeable>
      <van-tab v-for="item in Mychannels" :key="item.id" :title="item.name">
        <article-list :channel-id="item.id"></article-list>
      </van-tab>
      <span class="toutiao toutiao-gengduo" @click="isShowEdit = true"></span>
    </van-tabs>
    <van-popup
      v-model="isShowEdit"
      position="bottom"
      :style="{ height: '100%', width: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <edit-popup
        :my-channels="Mychannels"
        @update-active=";[(active = $event), (isShowEdit = false)]"
        @delete-channel="delMyChannels"
        @add-channel="addMyChannels"
      ></edit-popup>
    </van-popup>
  </div>
</template>

<script>
import { getChannelsAPI, delChannelsAPI, addChannelsAPI } from '@/api'
import ArticleList from './components/ArticleList.vue'
import EditPopup from '@/views/Home/components/Editpopup'
import { mapGetters, mapMutations } from 'vuex'
export default {
  name: 'Home',
  components: {
    ArticleList,
    EditPopup
  },
  data() {
    return {
      active: 1,
      Mychannels: [],
      isShowEdit: false
    }
  },
  created() {
    // this.getMyChannel()
    this.initMyChannels()
  },
  computed: {
    ...mapGetters(['isLogin'])
  },
  methods: {
    ...mapMutations(['SET_CHANNELS']),
    async initMyChannels() {
      // myChannels初始化逻辑
      if (this.isLogin) {
        /*
        登录：发送请求，携带token
        */
        this.getMyChannel()
      } else {
        /*
        未登录：
        1、数据库有数据，用数据库
        2、数据库没有数据，发送请求，不携带token，再把我的频道存储到本地
        */
        const { myChannels } = this.$store.state
        if (myChannels.length > 0) {
          this.Mychannels = myChannels
        } else {
          await this.getMyChannel()
          this.SET_CHANNELS(this.Mychannels)
        }
      }
    },
    async getMyChannel() {
      const { data } = await getChannelsAPI()
      this.Mychannels = data.data.channels
      // console.log(this.Mychannels)
    },
    async delMyChannels(id) {
      try {
        // 1、loading提示框
        this.$toast.loading({
          forbidClick: true,
          message: 'deleting...'
        })
        //提取
        const newChannels = this.Mychannels.filter((item) => item.id !== id)
        if (this.isLogin) {
          // 如果登录了，发送请求，删数据库的频道
          await delChannelsAPI(id)
        } else {
          //如果未登录，将删除好的我的频道的数组存到本地存储
          this.SET_CHANNELS(newChannels)
        }
        // 3、视图层删除频道
        this.Mychannels = this.Mychannels.filter((item) => item.id !== id)
        this.$toast.success('删除频道成功')
      } catch (error) {
        this.$toast.fail('删除频道失败')
      }
    },
    async addMyChannels(mychannel) {
      try {
        // 1、loading提示框
        this.$toast.loading({
          forbidClick: true,
          message: 'adding...'
        })
        // 2、发送请求
        if (this.isLogin) {
          //发送请求，添加数据库的频道
          await addChannelsAPI(mychannel.id, this.Mychannels.length)
        } else {
          //如果未登录，将添加好的我的频道的数组储存到本地
          this.SET_CHANNELS([...this.Mychannels, mychannel])
        }
        // 3、视图层添加频道
        this.Mychannels.push(mychannel)
        this.$toast.success('添加频道成功')
      } catch (error) {
        this.$toast.fail('添加频道失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
:deep(.navbar) {
  background-color: #3296fa;
  .van-nav-bar__title {
    max-width: unset;
  }
  .van-icon-search {
    color: #fff;
  }
  .search-btn {
    width: 7.4rem;
    height: 0.85333rem;
    color: #fff;
    background-color: #5babfb;
    // border: unset;
  }
}
/* tabs导航条样式 */
:deep(.van-tabs__wrap) {
  padding-right: 66px;

  .van-tabs__nav {
    padding-left: 0;
    padding-right: 0;

    /* tab标签 */
    .van-tab {
      border: 1px solid #eee;
      width: 200px;
      height: 82px;
    }

    /* tab标签下划线 */
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      bottom: 40px;
    }
  }
}

/* 字体图标 */
.toutiao-gengduo {
  position: absolute;
  top: 0;
  right: 0;
  width: 66px;
  height: 82px;
  font-size: 40px;
  line-height: 82px;
  text-align: center;
  opacity: 0.6;
  border-bottom: 1px solid #eee;

  &::after {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    height: 70%;
    width: 1px;
    background-image: url('~@/assets/images/gradient-gray-line.png');
  }
}
</style>
